/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb.metodos;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

/**
 *
 * @author pedro
 */
class HeaderFooter2 extends PdfPageEventHelper {

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            Rectangle page = document.getPageSize();

            PdfPTable head = new PdfPTable(new float[]{0.2f, 0.6f, 0.2f});

            Image image = Image.getInstance("C:\\Users\\pedro\\Documents\\NetBeansProjects\\Sigef\\Sigef-ejb\\src\\java\\META-INF\\federal.png");
            image.scalePercent(60.0f);
            PdfPCell cell = new PdfPCell(image);
            cell.setBorder(0);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            head.addCell(cell);

            Phrase phrase = new Phrase("Serviço Público Federal\n"
                    + "Ministério da Educação\n"
                    + "Fundação Universidade Federal de Mato Grosso do Sul", FontFactory.getFont(FontFactory.TIMES_BOLD, 10));
            cell = new PdfPCell(phrase);
            cell.setBorder(0);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            head.addCell(cell);

            image = Image.getInstance("C:\\Users\\pedro\\Documents\\NetBeansProjects\\Sigef\\Sigef-ejb\\src\\java\\META-INF\\ufms.png");
            image.scalePercent(60.0f);
            cell = new PdfPCell(image);
            cell.setBorder(0);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            head.addCell(cell);


            head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight() + 20, writer.getDirectContent());
            //System.out.printf("%f - %f - %f - %f\n", page.getHeight(), document.topMargin(), head.getTotalHeight(), page.getHeight() - document.topMargin() + head.getTotalHeight());
            PdfPTable foot = new PdfPTable(1);
            phrase = new Phrase("Pró-Reitoria de Ensino de Graduação/PREG\n"
                    + "Cidade Universitária – Caixa Postal 549 - Fone: (0xx67) 3345-7172 – Fax: (0xx67) 3345-7150\n"
                    + "79070-900 – Campo Grande(MS) - http://www.ufms.br – e-mail: diln.preg@ufms.br", FontFactory.getFont(FontFactory.TIMES_ROMAN, 9));

            cell = new PdfPCell(phrase);
            cell.setBorder(0);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            foot.addCell(cell);

            foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent());

        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }

    }
}
